package com.example.myapplication;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.adapter.VideoAdapter;
import com.example.myapplication.utils.Request;

import org.json.JSONArray;

/**
 * Created by caobotao on 16/1/4.
 */
public class FrdFragment extends Fragment {
    private View view;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.tab2, container, false);
        //new InitUI(view).execute();
        /*Intent intent = new Intent(getActivity(),Play.class);
        startActivity(intent);*/
        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser){
            new InitUI(view).execute();
        }
    }

    private  class InitUI extends AsyncTask<String, Integer, JSONArray> {
        private View view;
        InitUI (View view){
            this.view=view;
        }
        @Override
        protected JSONArray doInBackground(String... strings) {
            return Request.jsonarr("http://101.132.147.216/paa/videolist");
        }

        @Override
        protected void onPostExecute(JSONArray jsonarr) {
            RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.video_view);
            recyclerView.setLayoutManager(new GridLayoutManager(view.getContext(),2));
            recyclerView.setAdapter(new VideoAdapter(jsonarr));
        }
    }
}
